#pragma once
#include <iostream>
#include <fstream>
#include "PersonList.h"
#include "CoupleList.h"

class Dating
{
protected:
	PersonList<Person> Girls;
	PersonList<Person> Boys;
	CouplesList<Couple> Couples;
	int SameInterests;

public:

	Dating(void)
	{

	}

	void MatchingInterests()
	{
		cout << "Please enter a number(between 1-5) of interests to match: ";
		cin >> SameInterests;
		if (SameInterests <= 0)
		{
			SameInterests = 1;
		}
		if (SameInterests > 5)
		{
			SameInterests = 5;
		}
	}

	void MatchingInterests(int nr)
	{
		SameInterests = nr;
	}

	int Match(Person girl, Person boy)
	{
		int matches = 0;
		if(girl.getCoding() && boy.getCoding())
			matches++;
		if(girl.getHorses() && boy.getHorses())
			matches++;
		if(girl.getMagic() && boy.getMagic())
			matches++;
		if(girl.getParty() && boy.getParty())
			matches++;
		if (girl.getYoga() && boy.getYoga())
			matches++;
		return matches;
	}

	void PairCouple()
	{
		bool paired = false;
		int pairs = 0;
		while (!paired)
		{
			for (auto &girl : Girls)
			{
				for (auto &boy : Boys)
				{
					if (Match(girl.info, boy.info) >= SameInterests)
					{
						Couples.add_at_head(Couple(girl.info, boy.info));
						Girls.remove(&girl);
						Boys.remove(&boy); 
						paired = true;
						break;
					}
				}
				if(paired)
					break;
			}
			paired = false;
			pairs++;
			if (pairs == 3)
			{
				break;
			}

		}
		for(auto &c : Couples)
		{
			cout << "[" << c.info.getGirl().getName() << " och " << c.info.getBoy().getName() << "]" << endl;
		}
	}

	void readFile(string textfile)
	{
		ifstream fiffy;
		string row;
		fiffy.open(textfile);
		if (!fiffy.good())
		{
			cout << "Can't Open" << endl;
			return;
		}
		while (getline(fiffy, row))
		{
			int temp;
			Person p;
			istringstream iss(row);
			getline(iss, p.Geschlecht, ',');
			getline(iss, p.Nom, ',');
			iss >> temp;
			if (temp == 1)
			{
				p.setCoding(1);
			}
			iss >> temp;
			if (temp == 1)
			{
				p.setHorses(1);
			}
			iss >> temp;
			if (temp == 1)
			{
				p.setMagic(1);
			}
			iss >> temp;
			if (temp == 1)
			{
				p.setParty(1);
			}
			iss >> temp;
			if (temp == 1)
			{
				p.setYoga(1);
			}
			if (p.Geschlecht == "M")
			{
				Boys.add_at_head(p);
			}
			else
			{
				Girls.add_at_head(p);
			}
		}
		fiffy.close();
	}
	~Dating(void)
	{
	}
};

